package samples
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import marcel.display.draw.DrawManager;
	import marcel.display.draw.props.FillProps;
	import marcel.display.gui.buttons.CoreButton;
	import marcel.display.gui.buttons.SimpleTextButton;
	import marcel.display.gui.texts.CoreField;
	import marcel.utils.Aligner;
	import marcel.utils.Delegate;
	import marcel.utils.Pagination;
	
	public class PagerSample extends Sprite
	{
		protected var _containerSimple:Sprite = new Sprite();
		protected var _spritesSimple:Array = new Array();
		protected var _pagerSimple:Pagination;
		
		protected var _containerConcat:Sprite = new Sprite();
		protected var _spritesConcat:Array = new Array();
		protected var _pagerConcat:Pagination;
		
		protected var _nbElements:int = 23;
		protected var _stepSimple:int = 4;
		protected var _stepConcat:int = 5;
		protected var _loop:Boolean = true;
		
		
		public function PagerSample() {
			super();
			x = y = 100;
			_initElements();
			_initNavigation();
			_initPagers();
			_refresh();
		}
			
		protected function _initElements():void {
			addChild(_containerSimple);
			_containerSimple.y = 40;
			addChild(_containerConcat);
			_containerConcat.y = 200;
			for (var i:int = 1; i <= _nbElements; i++) {
				_spritesSimple.push(_getSprite(String(i), 100));
				_spritesConcat.push(_getSprite(String(i), 100));
			}
		}
		
		protected function _initNavigation():void {
			var _nav:Array = new Array();
			var _cb:CoreButton = _getButton("<<");
			_cb.addEventListener(MouseEvent.CLICK, Delegate.create(_goToPage, 0));
			_nav.push(addChild(_cb));
			
			_cb = _getButton("<");
			_cb.addEventListener(MouseEvent.CLICK, Delegate.create(_setPage, -1));
			_nav.push(addChild(_cb));
			
			var _nbPages:Number = Math.ceil(_nbElements / _stepSimple);
			for (var i:int = 0; i < _nbPages; i++) {
				_cb = _getButton(String(i + 1));
				_cb.addEventListener(MouseEvent.CLICK, Delegate.create(_goToPage, i));
				_nav.push(addChild(_cb));
			}
			_cb = _getButton(">");
			_cb.addEventListener(MouseEvent.CLICK, Delegate.create(_setPage, 1));
			_nav.push(addChild(_cb));
			
			_cb = _getButton(">>");
			_cb.addEventListener(MouseEvent.CLICK, Delegate.create(_goToPage, _nbPages - 1));
			_nav.push(addChild(_cb));
			
			Aligner.cascadeX(_nav, 0, 1);
			
			_nav = new Array();
			_cb = _getButton("<");
			_cb.y = 160;
			_cb.addEventListener(MouseEvent.CLICK, Delegate.create(_setPageConcat, -1));
			_nav.push(addChild(_cb));
			
			_cb = _getButton(">");
			_cb.y = 160;
			_cb.addEventListener(MouseEvent.CLICK, Delegate.create(_setPageConcat, 1));
			_nav.push(addChild(_cb));
			
			Aligner.cascadeX(_nav, 0, 1);
		}
		
		protected function _setPage(e:MouseEvent, dir:Number):void {
			dir === 1 ? _pagerSimple.next() : _pagerSimple.prev();
			_refresh();
		}
		
		protected function _setPageConcat(e:MouseEvent, dir:Number):void {
			dir === 1 ? _pagerConcat.next() : _pagerConcat.prev();
			_refresh();	
		}
		
		protected function _goToPage(e:MouseEvent, index:int):void {
			_pagerSimple.goToPage(index);
			_refresh();
		}
		
		protected function _initPagers():void {
			_pagerSimple = new Pagination(_nbElements, _stepSimple, true, false);
			_pagerConcat = new Pagination(_nbElements, _stepConcat, _loop, true);
		}
		
		protected function _getButton(s:String):CoreButton {
			return new SimpleTextButton(s, 0xFFFFFF, Math.round(Math.random() * 0xFFFFFF),Math.round(Math.random() * 0xFFFFFF));
		}
		
		protected function _getSprite(s:String, size:Number = 30):Sprite {
			var _s:Sprite = new Sprite();
			_s.mouseEnabled = false;
			_s.mouseChildren = true;
			DrawManager.drawRect(_s, new Rectangle(0,0,size,size), new FillProps(Math.round(Math.random() * 0xFFFFFF), .75));
			var _cf:CoreField = new CoreField(10,10,20,20);
			_cf.text = s;
			_cf.mouseEnabled = false;
			_s.addChild(_cf);
			return _s;
		}
		
		protected function _refresh():void {
			_clear();
			_refreshPager(_pagerSimple, _containerSimple, _spritesSimple);
			_refreshPager(_pagerConcat, _containerConcat, _spritesConcat);
		}
		
		private function _refreshPager(p:Pagination, container:Sprite, sprites:Array):void {
			var _index:Array = p.currentElements;
			var _elements:Array = new Array();
			for (var i:int = 0; i < _index.length; i++) _elements.push(container.addChild(sprites[_index[i]]));			
			Aligner.cascadeX(_elements, 0, 10);
		}
		
		protected function _clear():void {
			while(_containerSimple.numChildren > 0) _containerSimple.removeChildAt(0);
			while(_containerConcat.numChildren > 0) _containerConcat.removeChildAt(0);
		}
		
	}
}